package net.entelijan.config.impl2;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;

import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.filesel.DefaultFileselInitializer;
import net.entelijan.cobean.filesel.FileSelectionMode;
import net.entelijan.cobean.util.CobeanUtil;

public class DefaultFileselInitializerBinderFactory extends
		AbstractBinderFactory {

	public DefaultFileselInitializerBinderFactory() {
		super();
	}

	@Override
	protected List<List<String>> adjustPathesOfCoreComponent(List<List<String>> pathes) {
		return PathUtil.getHeads(pathes, "fileSel");
	}

	@Override
	protected boolean isCoreComponent(IEnhancedComponent comp) {
		return comp.getComponent() instanceof JComboBox
				&& (PathUtil.containsAnyKeywordIgnoreCase(comp.getPathes(),
						"fileSelField"));
	}

	@Override
	protected ICobean<?> createCobean(IEnhancedComponent coreComp,
			List<IEnhancedComponent> allComps, Class<?> modelPropertyType, Object config) {
		JComboBox core = (JComboBox) coreComp.getComponent();
		JButton chooser = findComponent(allComps, coreComp, JButton.class,
				"fileSelChooser");
		DefaultFileselInitializer ini = new DefaultFileselInitializer();
		ini.setCoComboBox(core);
		ini.setCoButton(chooser);
		ini.setId(CobeanUtil.createIdForComponent(core));
		if (PathUtil.containsAnyKeywordIgnoreCase(coreComp.getPathes(),
				"directory")) {
			ini.setFileSelectionMode(FileSelectionMode.DIRECTORIES_ONLY);
		}
		return ini.initialize();
	}

}
